<script setup>
import { computed, ref } from 'vue'
import { deletePatient, getPatientListInfo } from '@/apis/user'
import {onShow} from '@dcloudio/uni-app'
import { useConsultStore } from '@/stores'

// 侧滑按钮配置
const swipeOptions = ref([
  {
    text: '删除',
    style: {
      backgroundColor: '#dd524d'
    }
  }
])
//定义页面显示
const pageShow=ref(false)

//定义患者列表响应式数据
const patientList=ref([])

//获取患者列表
const getPatientList=async()=>{
	const res=await getPatientListInfo()
	patientList.value=res.data
	pageShow.value=true
}

//删除患者
const onDelete=async(id)=>{
	await deletePatient(id)
	uni.showToast({
		title:'删除患者成功'
	})
	getPatientList()
}

onShow(()=>{
	getPatientList()
})
//定义高亮下标
const activeIndex=ref(0)
//获取高亮
const onActiveIndex=(value)=>{
	activeIndex.value=value
}
//根据高亮下标获取患者id
const patientId=computed(()=>{
	return patientList.value[activeIndex.value].id
})

const store=useConsultStore()
//点击下一步
const onNext=()=>{
	if(!patientId.value){
		return uni.showToast({
			icon:'none',
			title:"请选择患者"
		})
	}
	// 存储患者 ID 并跳转页面
	store.setPatientId(patientId.value)
	uni.navigateTo({
		url:`/subpkg_consult/payment/index?id=${patientId.value}`
	})
}
</script>
<template>
  <scroll-page>
    <view class="patient-page" v-if="pageShow">
      <view class="page-header">
        <view class="patient-title"> 请选择患者信息 </view>
        <view class="patient-tips">
          以便医生给出更准确的治疗，信息仅医生可见
        </view>
      </view>
      <uni-swipe-action >
        <uni-swipe-action-item :right-options="swipeOptions" v-for="(item,index) in patientList" :key="item.id" @click="onDelete(item.id)">
          <view class="archive-card" :class="{active:activeIndex===index}" @click="onActiveIndex(index)">
            <view class="archive-info">
              <text class="name">{{item.name}}</text>
              <text class="id-card">{{item.idCard.replace(/^(\d{6})\d{8}(\w{4})$/,"$1********$2")}}</text>
              <text class="default" v-if="item.defaultFlag===1">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{item.genderValue}}</text>
              <text class="age">{{item.age}}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/index?id=${item.id}`"
							
            >
              <uni-icons
                type="icon-edit"
                size="20"
                color="#16C2A3"
                custom-prefix="iconfont"
              />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patientList.length<6" class="archive-card">
        <navigator
          class="add-link"
          hover-class="none"
          url="/subpkg_archive/form/index"
        >
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
    </view>
    <!-- 下一步操作 -->
    <view class="next-step">
      <button class="uni-button" @click="onNext">下一步</button>
    </view>
  </scroll-page>
</template>

<style lang="scss">
@import './index.scss';
</style>